package cn.echcz.restboot.controller;

import cn.echcz.restboot.auth.AuthContext;
import cn.echcz.restboot.auth.Authentication;
import cn.echcz.restboot.model.User;
import cn.echcz.restboot.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/users")
@Api("用户")
public class UserApi {
    @NonNull
    private UserService userService;

    @ApiOperation("查询所有用户")
    @GetMapping
    public List<User> getAll() {
        return userService.findAll();
    }

    @ApiOperation("查看自己的信息")
    @GetMapping("/self")
    public User getSelf() {
        Authentication authentication = AuthContext.getAuthentication();
        return userService.findDetailById((Integer) authentication.getSubject());
    }
}
